package com.shen.controller;

import com.shen.entity.TrainInfo;
import com.shen.service.TrainService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TrainController {
    @Autowired
    TrainService trainService;

    @RequestMapping("/")
    public String getAllTrain(Model model){
        List<TrainInfo> allTrain = trainService.getAllTrain();
        model.addAttribute("allTrain",allTrain);
        return "trainList";
    }

    @RequestMapping("/train/{id}")
    public String getTrainById(@PathVariable("id")String id,Model model){
        TrainInfo train = trainService.findTrain(id);
        model.addAttribute("train",train);
        return "trainInfo";
    }

    @RequestMapping("/train/del")
    @ResponseBody
    public String delTrain(@RequestParam("ids")String[] ids){
        for (String id : ids) {
            System.out.println(id);
        }
        int i = trainService.delTrain(ids);
        if(i>0){
            return "删除成功！";
        }else{
            return "删除失败！";
        }

    }
}
